<template>
    <div>
        <div class="container">
            <TableCustom :columns="columns" :tableData="tableData" :viewFunc="handleView"
                :delFunc="handleDelete" :total="page.total" :has-pagination="false"  :editFunc="handleEdit" :refresh="getData" style="height: calc(100vh - 250px);">
                <template #toolbarBtn>
                    <el-button type="warning" :icon="CirclePlusFilled" @click="visible = true">新增</el-button>
                </template>
            </TableCustom>
        </div>
        <el-dialog :title="isEdit ? '编辑' : '新增'" v-model="visible" width="700px" destroy-on-close
            :close-on-click-modal="false" @close="closeDialog">
            <TableEdit :form-data="rowData" :options="options" :edit="isEdit" :update="updateData" />
        </el-dialog>
        <el-dialog title="查看详情" v-model="visible1" width="700px" destroy-on-close>
            <TableDetail :data="viewData"></TableDetail>
        </el-dialog>
    </div>
    
</template>

<script setup lang="ts" name="company-management">
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { CirclePlusFilled } from '@element-plus/icons-vue';
import { Company } from '@/types/dto';
import { fetchCompanyData,postCompany,delCompanyDataById } from '@/api';
import TableCustom from '@/components/table-custom.vue';
import TableDetail from '@/components/table-detail.vue';
import { FormOption, FormOptionList } from '@/types/form-option';

// 表格相关
let columns = ref([
    { type: 'id', prop : 'labelId', label: '序号', width: 55, align: 'center' },
    { prop: 'companyName', label: '公司名称' },
    { prop: 'contactPerson', label: '联系人' },
    { prop: 'contactPhone', label: '联系电话' },
    { prop: 'address', label: '地址' },
    { prop: 'operator', label: '操作', width: 250 },
])
const page = reactive({
    total: 0,
    hasPagination: false
})
const tableData = ref<Company[]>([]);
const getData = async () => {
    const res = await fetchCompanyData()
    tableData.value = res.data;
    tableData.value.forEach((item, index) => {
        item.labelId = index + 1;
    })
    page.total = res.data.length;
};
const saveCompany = async (data: Company) => {
    // 这里可以添加调用API的逻辑来新增公司
    await postCompany(data); 
    getData();
};

const deleteCompany = async (id) => {
    // 这里可以添加调用API的逻辑来新增公司
    await delCompanyDataById(id); 
    getData();
};
getData();


// 新增/编辑弹窗相关
let options = ref<FormOption>({
    labelWidth: '100px',
    span: 12,
    list: [
        { type: 'input', label: '序号', prop: 'id', required: false, disabled: true},
        { type: 'input', label: '公司名称', prop: 'companyName', required: true },
        { type: 'input', label: '联系人', prop: 'contactPerson', required: false },
        { type: 'input', label: '联系电话', prop: 'contactPhone', required: false },
        { type: 'input', label: '地址', prop: 'address', required: false }
    ]
})
const visible = ref(false);
const isEdit = ref(false);
const rowData = ref({});
const handleEdit = (row: Company) => {
    rowData.value = { ...row };
    isEdit.value = true;
    visible.value = true;
};
const updateData = async (data: Company) => {
    await saveCompany(data);
    if (!isEdit.value) {
        ElMessage.success('新增成功');
    } else {
        ElMessage.success('编辑成功');
    }
    closeDialog();
    getData();
};

const closeDialog = () => {
    visible.value = false;
    isEdit.value = false;
};

// 查看详情弹窗相关
const visible1 = ref(false);
const viewData = ref({
    row: {},
    list: []
});
const handleView = (row: Company) => {
    viewData.value.row = { ...row }
    viewData.value.list = [
        { type: 'id', label: '序号' },
    { prop: 'companyName', label: '公司名称' },
    { prop: 'contactPerson', label: '联系人' },
    { prop: 'contactPhone', label: '联系电话' },
    { prop: 'address', label: '地址' }
    ]
    visible1.value = true;
};

// 删除相关
const handleDelete = (row: Company) => {
    deleteCompany(row.id);
    ElMessage.success('删除成功');
}
</script>

<style>

.container {
    min-height: calc(100vh - 150px) !important; 
    box-sizing: border-box;
    background: #fff;
    padding: 20px;
    overflow: auto;
}

</style>